<template>
  <div class="todo-footer">
    <label>
      <!-- <input type="checkbox" :checked="todoStore.isDoneNum === todoStore.totalNum && !!todoStore.isDoneNum" @change="checkAll" /> -->
      <input type="checkbox" v-model="checkAll" />
    </label>
    <span>
      <span>已完成 {{ todoStore.isDoneNum }}</span> / 全部 {{ todoStore.totalNum }}
    </span>
    <button class="btn btn-danger" v-show="!!todoStore.isDoneNum" @click="delIsDoneTodo">清除已完成任务</button>
  </div>
</template>

<script lang="ts">
export default {
  name: "Footer",
};
</script>

<script lang="ts" setup>
import { computed } from "vue";
import { useTodoStore } from "../../pinia/modules/todo";

const todoStore = useTodoStore();

// 全选&全不选
// const checkAll = (e: Event) => {
//   // e.target是联合类型，需要断言成具体的类型，才能读取属性和方法
//   const isDone = (e.target as HTMLInputElement).checked;

//   todoStore.todoList.forEach((todo) => {
//     todo.isDone = isDone;
//   });
// };

const checkAll = computed({
  get() {
    return todoStore.isDoneNum === todoStore.totalNum && !!todoStore.isDoneNum;
  },
  set(val) {
    todoStore.todoList.forEach((todo) => {
      todo.isDone = val;
    });
  },
});

// 删除已完成的todo
const delIsDoneTodo = () => {
  todoStore.todoList = todoStore.todoList.filter((todo) => !todo.isDone);
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
